//
//  SAGlobalTool.h
//  ShopAssistant
//
//  Created by FangGeek on 2017/11/16.
//  Copyright © 2017年 hyxt. All rights reserved.
//

#ifndef SAGlobalTool_h
#define SAGlobalTool_h
#define SA_SYSTEM_VERSION                              [[[UIDevice currentDevice] systemVersion] floatValue]

#define SA_SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)

#define SA_SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)

#define SA_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

#define SA_SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)

#define SA_SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

#define SA_SCREEN_SIZE      [[UIScreen mainScreen] bounds].size                 //(e.g. 320,480)
#define SA_SCREEN_WIDTH     [[UIScreen mainScreen] bounds].size.width           //(e.g. 320)
#define SA_SCREEN_HEIGHT    [[UIScreen mainScreen] bounds].size.height          //包含状态bar的高度(e.g. 480)

#define SA_APPLICATION_SIZE   [[UIScreen mainScreen] applicationFrame].size       //(e.g. 320,460)
#define SA_APPLICATION_WIDTH  [[UIScreen mainScreen] applicationFrame].size.width //(e.g. 320)
#define SA_APPLICATION_HEIGHT [[UIScreen mainScreen] applicationFrame].size.height - 44//不包含状态bar和导航栏的高度(e.g. 416)

#define SA_STATUS_BAR_HEIGHT  (([[[UIDevice currentDevice] systemVersion] intValue]) > 6 ? 20 : 0)
#define SA_NAVIGATION_BAR_HEIGHT 44

#define SA_UNDER_STATUS_BAR_START_Y (kDevice_iPhoneX ? 44 : 20) //

#define SA_CONTENT_START_Y    (kDevice_iPhoneX ? 88 : 64) //

#define SA_CONTENT_HEIGHT     (SA_SCREEN_HEIGHT - SA_NAVIGATION_BAR_HEIGHT - SA_UNDER_STATUS_BAR_START_Y)

//系统字体
#define SA_SYS_FONT(x) [UIFont systemFontOfSize:x]
#define FJK_BOLD_FONT(x) [UIFont fontWithName:@"Helvetica-Bold" size:(x)]   //加粗字体

#define kDevice_iPhoneX CGSizeEqualToSize(CGSizeMake(375, 812), [[UIScreen mainScreen] bounds].size)

//Color工具
#define SARGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define SARGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f \
alpha:(a)]

#define SAHSVCOLOR(h,s,v) [UIColor colorWithHue:(h) saturation:(s) value:(v) alpha:1]
#define SAHSVACOLOR(h,s,v,a) [UIColor colorWithHue:(h) saturation:(s) value:(v) alpha:(a)]

//rgb颜色转换（16进制->10进制）
#define SAHEXRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define SA_COLOR_WITH_RGBA(R,G,B,A) ([UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A])



//系统缓存目录
#define SA_DOCUMENT_DIR NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject
#define SA_CACHE_DIR NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject

//获取当前版本号
#define SACurrentVersion [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"]

// 常用宏定义
#define SAGetStringDefaultEmpty(string) ([string isKindOfClass:[NSString class]] && string && [string length] > 0 ? string : @"")
#define SAIsStringNilOrEmpty(string)   ((string == nil) || ([string isKindOfClass:[NSString class]] && [string length] == 0))
#define SAIsStringNotNil(string) ((string != nil) && [string isKindOfClass:[NSString class]] && [string length] > 0)
#define SACompareString(thing1, thing2) ([thing1 isKindOfClass:[NSString class]] && [thing2 isKindOfClass:[NSString class]] && [thing1 compare:thing2 options:NSCaseInsensitiveSearch] == NSOrderedSame)

// 比较字符串是否相等（忽略大小写），相等的话返回YES，否则返回NO
#define SACompareString(thing1, thing2) ([thing1 isKindOfClass:[NSString class]] && [thing2 isKindOfClass:[NSString class]] && [thing1 compare:thing2 options:NSCaseInsensitiveSearch] == NSOrderedSame)

//NSUserDefaults
#define SAUserDefaults  [NSUserDefaults standardUserDefaults]

//弱引用
#define SAWeakSelf   __weak typeof(self) weakSelf = self;
#define SAStrongSelf __strong typeof(weakSelf) strongSelf = weakSelf;

//弱引用
#define SAWeakObj(x)  __weak typeof(x) weakObj = x;
#define SAStrongObj __strong typeof(weakObj) strongObj = weakObj;

#endif
#ifndef    weakify
#if __has_feature(objc_arc)

#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x; \
_Pragma("clang diagnostic pop")

#else

#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __block __typeof__(x) __block_##x##__ = x; \
_Pragma("clang diagnostic pop")

#endif
#endif

#ifndef    strongify
#if __has_feature(objc_arc)

#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __weak_##x##__; \
_Pragma("clang diagnostic pop")

#else

#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __block_##x##__; \
_Pragma("clang diagnostic pop")

#endif

#endif /* SAGlobalTool_h */
